<!--
 * @Author: luojiajun
 * @Email: company.kachun@gmail.com
 * @Date: 2021-07-03 15:02:29
 * @FilePath: /src/views/service/application/mine.vue
 * @LastEditTime: 2021-07-17 17:19:01
 * @LastEditors: null
-->
<template>
    <div class="app-container app-height">
        <div class="bg-white">
            <!-- TODO 整一块需要根据不同的tab筛序需要的搜索项 -->
            <el-form
                ref="ruleForm"
                :model="ruleForm"
                label-width="110px"
                class="application-form clearfix"
            >
                <el-form-item
                    v-if="activeName == 1 || activeName == 2"
                    label="服务中文名称"
                    prop="apiName"
                    class="float-left"
                >
                    <el-input
                        v-model="ruleForm.apiName"
                        placeholder="请输入服务中文名称"
                        clearable
                    ></el-input>
                </el-form-item>
                <el-form-item
                    v-if="activeName == 1 || activeName == 2"
                    label="接口所属组"
                    prop="groupId"
                    class="float-left"
                >
                    <el-select
                        v-model="ruleForm.groupId"
                        placeholder="请选择接口所属组"
                        clearable
                    >
                        <el-option
                            v-for="item in groupList"
                            :label="item.name"
                            :value="item.id"
                            :key="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item
                    v-if="activeName == 1 || activeName == 2 || activeName == 3"
                    label="选择时间"
                    prop="datetime"
                    class="float-left"
                >
                    <el-date-picker
                        v-model="ruleForm.datetime"
                        type="datetimerange"
                        range-separator="至"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        @change="changeDatetimePicker"
                        align="left"
                        clearable
                    >
                    </el-date-picker>
                </el-form-item>

                <el-form-item
                    v-if="activeName == 1"
                    label="服务英文"
                    prop="apiNameEn"
                    class="float-left"
                >
                    <el-input
                        v-model="ruleForm.apiNameEn"
                        placeholder="请输入服务英文名称"
                        clearable
                    ></el-input>
                </el-form-item>

                <el-form-item
                    v-if="activeName == 1 || activeName == 2"
                    label="接口调用路径"
                    prop="url"
                    class="float-left"
                >
                    <el-input
                        v-model="ruleForm.url"
                        placeholder="请输入接口调用路径"
                        clearable
                    ></el-input>
                </el-form-item>
                <el-form-item
                    v-if="activeName == 1"
                    label="状态"
                    prop="status"
                    class="float-left"
                >
                    <el-select
                        v-model="ruleForm.status"
                        placeholder="请选择状态"
                        clearable
                    >
                        <!-- <el-option
                            v-for="key in Object.keys(
                                $enum.INVOKE_TRY_STATUS_MAP
                            )"
                            :key="key"
                            :label="$enum.INVOKE_TRY_STATUS_MAP[key]"
                            :value="key"
                        >
                        </el-option> -->
                        <el-option
                            v-for="item in statusList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item
                    v-if="activeName == 1 || activeName == 2 || activeName == 3"
                    label="用户名称"
                    prop="userName"
                    class="float-left"
                >
                    <el-input
                        v-model="ruleForm.userName"
                        placeholder="请输入用户名称"
                        clearable
                    ></el-input>
                </el-form-item>
                <el-form-item
                    v-if="activeName == 1 || activeName == 2 || activeName == 3"
                    label="用户所属组"
                    prop="userGroup"
                    class="float-left"
                >
                    <el-select
                        v-model="ruleForm.userGroup"
                        placeholder="用户所属组"
                        clearable
                    >
                        <el-option
                            v-for="item in userGroptList"
                            :label="item.name"
                            :value="item.id"
                            :key="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item
                    label-width="15px"
                    class="application-form-btn float-left"
                >
                    <el-button
                        type="primary"
                        icon="el-icon-search"
                        size="mini"
                        @click="fetchData"
                        >查询</el-button
                    >
                    <el-button type="primary" size="mini" @click="resetForm"
                        >重置</el-button
                    >
                </el-form-item>
            </el-form>

            <el-tabs v-model="activeName" @tab-click="handleClick">
                <el-tab-pane label="服务记录" name="1">
                    <ServiceLog
                        :form="ruleForm"
                        ref="serviceLogRef"
                    ></ServiceLog>
                </el-tab-pane>
                <el-tab-pane label="接口统计" name="2">
                    <InterfaceStatistics
                        :form="ruleForm"
                        ref="interfaceStatisticsRef"
                    ></InterfaceStatistics>
                </el-tab-pane>
                <el-tab-pane label="用户订阅统计" name="3">
                    <UserSubscriptionStatistics
                        :form="ruleForm"
                        ref="userSubscriptionStatisticsRef"
                    ></UserSubscriptionStatistics>
                </el-tab-pane>
            </el-tabs>
        </div>
    </div>
</template>

<script>
import { getUserApiGroup } from "@/api/dataManagement/system/user";
import { getUserGroup } from "@/api/dataService/serviceMonitor/monitorList";
import ServiceLog from "./modules/ServiceLog.vue";
import InterfaceStatistics from "./modules/InterfaceStatistics";
import UserSubscriptionStatistics from "./modules/UserSubscriptionStatistics";

export default {
    name: "MineApplication",
    components: {
        ServiceLog,
        InterfaceStatistics,
        UserSubscriptionStatistics
    },
    data() {
        return {
            activeName: "1",
            ruleForm: {
                apiName: "",
                groupId: "",
                datetime: [],
                apiNameEn: "",
                url: "",
                status: "",
                userName: "",
                userGroup: ""
                // viewType: 1 // 接口状态 查看类型 1.我的创建 2.我的发布 3.我的订阅
            },
            groupList: [],
            userGroptList: [],
            statusList: [
                {
                    id: 0,
                    name: "失败"
                },
                {
                    id: 1,
                    name: "成功"
                }
            ],
            pickerOptions: {}
        };
    },
    created() {
        this.getUserApiGroup();
        this.getUserGroup();
    },
    activated() {
        this.getUserApiGroup();
        this.getUserGroup();
    },
    methods: {
        handleClick(value) {
            // 清空，避免污染
            this.$refs.ruleForm.resetFields();
            this.ruleForm.startTime = "";
            this.ruleForm.endTime = "";
            value.name == 1 && this.$refs.serviceLogRef.fetchData();
            value.name == 2 && this.$refs.interfaceStatisticsRef.fetchData();
            value.name == 3 &&
                this.$refs.userSubscriptionStatisticsRef.fetchData();
        },
        // 获取分组列表
        getUserApiGroup() {
            getUserApiGroup().then(res => {
                if (res.code === 200) {
                    this.groupList = res.data;
                }
            });
        },
        getUserGroup() {
            getUserGroup().then(res => {
                if (res.code === 200) {
                    this.userGroptList = res.data;
                }
            });
        },
        changeDatetimePicker(data) {
            let startTime = "",
                endTime = "";
            if (data && data.length) {
                startTime = this.parseTime(
                    new Date(data[0]),
                    "{y}-{m}-{d} {h}:{i}:{s}"
                );
                endTime = this.parseTime(
                    new Date(data[1]),
                    "{y}-{m}-{d} {h}:{i}:{s}"
                );
            }
            this.ruleForm.startTime = startTime;
            this.ruleForm.endTime = endTime;
        },
        resetForm() {
            this.$refs.ruleForm.resetFields();
            this.ruleForm.startTime = "";
            this.ruleForm.endTime = "";
            this.fetchData();
        },
        fetchData() {
            this.activeName == 1 && this.$refs.serviceLogRef.fetchData();
            this.activeName == 2 &&
                this.$refs.interfaceStatisticsRef.fetchData();
            this.activeName == 3 &&
                this.$refs.userSubscriptionStatisticsRef.fetchData();
        }
    }
};
</script>

<style lang="scss" scoped>
.app-container {
    padding: 20px;

    .application-form ::v-deep {
        .el-form-item__label {
            text-align: right;
        }
    }
}
</style>
